package cn.yuanqiao.manage.service.impl;

import cn.yuanqiao.manage.domain.RyMjDzb;
import cn.yuanqiao.manage.mapper.MijiComparisonTableMapper;
import cn.yuanqiao.manage.service.IMijiComparisonTableService;
import cn.yuanqiao.system.domain.YqdaSysDictData;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *@Author: 赵常飞
 *@Date: 2024/6/4 10:41
 *@Description:
 *@Note:
 */
@Service
public class MijiComparisonTableServiceImpl implements IMijiComparisonTableService {
    @Autowired
    private MijiComparisonTableMapper mijiComparisonTableMapper;
    @Autowired
    private YqdaSysDictTypeServiceImpl yqdaSysDictTypeService;

    @Override
    public List<YqdaSysDictData> getrymjdict() {
        return yqdaSysDictTypeService.selectDictDataByType("RYMJ");
    }

    @Override
    public List<YqdaSysDictData> getdamjdict() {
        return yqdaSysDictTypeService.selectDictDataByType("MJ");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(JSONObject json) {
        JSONArray array= JSON.parseArray(json.getString("data"));
        mijiComparisonTableMapper.deleteAll();
        RyMjDzb ryMjDzb=new RyMjDzb();
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsoni=JSON.parseObject(String.valueOf(array.get(i)));
            String rymj=jsoni.getString("rymj");
            JSONArray damj = jsoni.getJSONArray("damj");
            for (int j = 0; j < damj.size(); j++) {
                ryMjDzb.setDaMj(String.valueOf(damj.get(j)));
                ryMjDzb.setRyMj(String.valueOf(rymj));
                mijiComparisonTableMapper.add(ryMjDzb);
            }

        }
        return 1;
    }

    @Override
    public List<RyMjDzb> getmjdzb() {
        return mijiComparisonTableMapper.getmjdzb();
    }
}
